//
//  ThemesView.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ThemesView.h"
//#import "ThemesTableCell.h"
#import "InfosTableCell.h"
#import "MoreButtonCell.h"
#import "WDUserMessage.h"
#import "ZHWebService.h"
#import "ZHDataService.h"
#import "MessageDetailViewController.h"
#import "PersonalViewController.h"
#import "WDFansAppDelegate.h"


@implementation ThemesView

@synthesize messages, nextPageExist, currentPage;

#pragma mark - my own methods
- (void)getPersonalMessagesWithPage:(NSInteger)page
{
    BOOL isReaded = YES;
    if(self.curTab == kPersonalThemesTab)
        isReaded = NO;
    
    CompletionBlock completion = ^(NSData *data)
    {
        NSDictionary *dict = [ZHDataService parseToPersonalMessagesWithData:data];
        self.currentPage++;
        if(self.currentPage == 2)
        {
            self.messages = [dict objectForKey:@"personalMessages"];
            if (isReaded==NO) {
                WDFansAppDelegate *appDelegate = (WDFansAppDelegate *)[UIApplication sharedApplication].delegate;
                NSArray* array = appDelegate.rootController.viewControllers;
                if ([array count]>1) {
                    UINavigationController* vc = [array objectAtIndex:1];
                    if ([self.messages count]>0) {
                        vc.tabBarItem.badgeValue = @"1";
                    }
                    else {
                        vc.tabBarItem.badgeValue = nil;
                    }
                }
                
            }
        }
        else
            [self.messages addObjectsFromArray:[dict objectForKey:@"personalMessages"]];
        NSString *hasPage = [dict objectForKey:@"page"];
        if(0 == [hasPage intValue])
            self.nextPageExist = NO;
        else
            self.nextPageExist = YES;
        [self.baseTableView reloadData];
    };
    ErrorBlock error = ^(NSString *error){};
    
    [[ZHWebService sharedService] getPersonalMessagesWithPage:page isReaded:isReaded onCompletion:completion onError:error];
}

#pragma mark init and dealloc

- (void)dealloc
{
    [messages release];
    [super dealloc];
}

#pragma mark - UITableViewDataSource methods
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        if(self.curTab == kPersonalThemesTab)
            return [NSString stringWithFormat:@"共%d条未读消息",[self.messages count]];
        else
            return [NSString stringWithFormat:@"共%d条已读消息",[self.messages count]];
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return kInfosTableCellHeight;
    }
    else
        return kMoreButtonCellHeight;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.nextPageExist) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return [self.messages count];
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        static NSString *cellIdentify = @"cellIdentify";
        InfosTableCell *cell = (InfosTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if(cell == nil)
        {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"InfosTableCell" owner:self options:nil];
            cell = (InfosTableCell *)[nibs lastObject];
        }
        WDUserMessage *m = [self.messages objectAtIndex:indexPath.row];
        cell.infoTitleLabel.text = m.mesTitle;
        return cell;
    }
    else
    {
        static NSString *cellIdentifyMore = @"cellIdentifyMore";
        MoreButtonCell *mbc = (MoreButtonCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifyMore];
        if(mbc == nil)
        {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"MoreButtonCell" owner:self options:nil];
            mbc = (MoreButtonCell *)[nibs lastObject];
        }
        return mbc;
    }
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        WDUserMessage *mes = [self.messages objectAtIndex:indexPath.row];
        PersonalViewController *personalController = (PersonalViewController *)[[[self superview] superview] nextResponder];
        MessageDetailViewController *messageController = [[MessageDetailViewController alloc] initWithMessage:mes];
        if(self.curTab == kPersonalThemesTab)
            messageController.isForRead = YES;
        else
            messageController.isForRead = NO;
        [personalController.navigationController pushViewController:messageController animated:YES];
        [messageController release];
    }
    else
    {
        [self getPersonalMessagesWithPage:self.currentPage];
    }
}
@end
